package com.wkk.log.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wkk.annotation.TokenCache;
import com.wkk.constant.CommonConstant;
import com.wkk.log.dto.LogQueryDto;
import com.wkk.log.dto.OperationDto;
import com.wkk.log.service.LogService;
import com.wkk.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * @author : WangKeKe
 * @date : 2020/9/28 15:22
 * Description : 用户操作日志控制类
 */

@Slf4j
@RestController
@RequestMapping("/log")
@Api(tags = "RESTFUL-管理员-日志操作")
@Validated
@TokenCache(isToken = true, keyToken = CommonConstant.USER_TOKEN_ID)
public class LogController {

    @Autowired
    private LogService logService;

    @ApiOperation("日志信息-分页条件查询")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "当前页码", name = "pageNo", required = true),
            @ApiImplicitParam(value = "每页大小", name = "pageSize", required = true),
    })
    @PostMapping("/getLogPage/{pageNo}/{pageSize}")
    public Result<Page<OperationDto>> queryLogPage(@PathVariable(name = "pageNo") Integer pageNo,
                                                   @PathVariable(name = "pageSize") Integer pageSize,
                                                   @RequestBody(required = false) @Validated LogQueryDto logQueryDto) {
        return Result.ok(logService.queryPage(pageNo, pageSize, logQueryDto));
    }

    @ApiOperation("日志信息-通过ID删除")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "日志ID,长度是19位", name = "id", required = true),
    })
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteLogById(@PathVariable(name = "id") @Length(min = 19, max = 19, message = "id的长度为必须是19位") String id) {
        return logService.removeById(id) ? Result.ok("删除成功") : Result.error("删除失败");
    }

}
